package jnpf.engine.custom.type;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jnpf.engine.custom.constants.GlobalProcessVariableConstants;
import jnpf.engine.entity.FlowTaskCirculateEntity;
import jnpf.engine.service.FlowTaskCirculateService;
import jnpf.engine.util.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.impl.event.FlowableEntityEventImpl;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.flowable.variable.service.event.impl.FlowableVariableEventImpl;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Doctor4JavaEE
 * @since 2024/2/18
 */
@Component
@Slf4j
public class VariableUpdatedListener implements FlowableEventListener {
    @Override
    public void onEvent(FlowableEvent event) {
        if(event instanceof org.flowable.variable.service.event.impl.FlowableVariableEventImpl){{
            FlowableVariableEventImpl flowableVariableEvent =(FlowableVariableEventImpl) event;
            String variableName = flowableVariableEvent.getVariableName();

            FlowTaskCirculateService flowTaskCirculateService = SpringContextHolder.getBean(FlowTaskCirculateService.class);
            if(GlobalProcessVariableConstants.PROCESS_STATUS.equals(variableName)){
                Integer variableValue = (Integer) flowableVariableEvent.getVariableValue();
                LambdaQueryWrapper<FlowTaskCirculateEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(FlowTaskCirculateEntity::getTaskId,flowableVariableEvent.getProcessInstanceId());
                List<FlowTaskCirculateEntity> list =
                        flowTaskCirculateService.list(lambdaQueryWrapper);
                for (FlowTaskCirculateEntity flowTaskCirculateEntity : list) {
                    flowTaskCirculateEntity.setProcessStatus(variableValue);
                }
                flowTaskCirculateService.updateBatchById(list);
            }
            else if(GlobalProcessVariableConstants.FLOW_URGENT.equals(variableName)){
                Integer variableValue = (Integer) flowableVariableEvent.getVariableValue();
                LambdaQueryWrapper<FlowTaskCirculateEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(FlowTaskCirculateEntity::getTaskId,flowableVariableEvent.getProcessInstanceId());
                List<FlowTaskCirculateEntity> list =
                        flowTaskCirculateService.list(lambdaQueryWrapper);
                for (FlowTaskCirculateEntity flowTaskCirculateEntity : list) {
                    flowTaskCirculateEntity.setFlowUrgent(variableValue);
                }
                flowTaskCirculateService.updateBatchById(list);
            }
        }

        }
    }

    @Override
    public boolean isFailOnException() {
        return true;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}
